

$NG.AllReady(function (page, { useValuesChange,useDataIndexChange,useUpdateRows,useUpdateRow }) {

    const mstform = $NG.getCmpApi("p_form_visa_data_m")

    if(page.oType=='add'||page.oType=='edit'){
        getPtInfoByPhid()
        getCntCount("p_form_visa_data_m","u_cnt",()=>{
            getCntMInfoByPhid()
        })
    }
    useValuesChange(({args})=>{
        getPtInfoByPhid()
        setTitle()
        getCntCount("p_form_visa_data_m","u_cnt",()=>{
            getCntMInfoByPhid()
        })
    },"p_form_visa_data_m.phid_pc")

    useValuesChange(({args})=>{
        getCntMInfoByPhid()
    },"p_form_visa_data_m.u_cnt")
    function getPtInfoByPhid() {
        if (mstform.getItem("phid_pc").getValue()) {
          $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(datas);
            $NG.updateState((updater) => {
              updater.data.p_form_visa_data_m.setProps({
                //计划开工日期
                u_start_dt: datas.start_date,
                //计划完工日期
                u_end_dt: datas.end_date,
              });
            });
          });
        }
      }
          setTitle()
    function setTitle(){
        $NG.execServer("GetVisaTime",{phid_pc:mstform.getItem("phid_pc").getValue()},function(res){
            console.log(res);
            if(res.count ==0){
                $NG.updateState((updater)=>{
                    updater.data.p_form_visa_data_m.setProps({
                        u_zt:`${mstform.getItem("phid_pc").getText()}第1次签证`
                    })
                })
            }else{
                const _data = JSON.parse(res.data)[0].extendObjects
                $NG.updateState((updater)=>{
                    updater.data.p_form_visa_data_m.setProps({
                        u_zt:`${mstform.getItem("phid_pc").getText()}第${(_data.count)+1}次签证`
                    })
                })
            }
            
        })
    }
    function getCntMInfoByPhid(params) {
        $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_cnt").getValue() }, function (res) {
          const datas = JSON.parse(res.data)[0].extendObjects;
          console.log(datas);
          $NG.updateState((updater) => {
            updater.data.p_form_visa_data_m.setProps({
              u_cnt_no: datas.pcm_no, //合同编号
              start_dt: datas.start_dt, //约定开工时间
              end_dt: datas.end_dt, //约定完工时间
              cnt_sum_vat_fc: datas.cnt_sum_vat_fc, //合同价税合计
              //产品线
              user_product_line: { value: datas.midwife_phid, label: datas.midwife_name },
              //合同类型
              u_cnt_type: { value: datas.pct_id, label: datas.pct_name },
              //甲方单位
              u_customer: { value: datas.customer_id, label: datas.customer_name },
              //乙方单位
              supply: { value: datas.supply_id, label: datas.supply_name },
              //发票类型
              u_invoice_type: { value: datas.invoice_type_phid, label: datas.invoice_type_name },
              //购方信息（客户信息）（收票单位信息）
              //单位名称
              tick_id: { value: datas.tick_id, label: datas.tick_name },
              //单位编码
              tick_no: datas.tick_no,
              //社会信用代码
              tick_unisocialcredit: datas.tick_unisocialcredit,
              //单位地址
              tick_taxaddress: datas.tick_taxaddress,
              //联系电话
              tick_taxtelephone: datas.tick_taxtelephone,
              //银行名称
              tick_taxbankid: { value: datas.tick_taxbankid, label: datas.tick_taxbankname },
              //银行账号
              tick_taxaccountno: datas.tick_taxaccountno,
              //销方信息（供应商信息）（开票单位信息）
              //单位名称
              inv_id: { value: datas.inv_id, label: datas.inv_name },
              //单位编码
              inv_no: datas.inv_no,
              //社会信用代码
              inv_unisocialcredit: datas.inv_unisocialcredit,
              //单位地址
              inv_taxaddress: datas.inv_taxaddress,
              //联系电话
              inv_taxtelephone: datas.inv_taxtelephone,
              //银行名称
              inv_taxbankid: { value: datas.inv_taxbankid, label: datas.inv_taxbankname },
              //银行账号
              inv_taxaccountno: datas.inv_taxaccountno,
            });
          });
        });
      }
      function cntMFilter() {
        $NG.updateUI((updater, state) => {
          updater.fieldSetForm.p_form_equipment_chg.u_cnt_id.setProps({
            onBeforeOpen: async () => {
              var phid_pc = mstform.getItem("phid_pc").getValue();
              if (!phid_pc) {
                $NG.alert("请先选择项目");
                return false;
              } else {
                return true;
              }
            },
            ...(!!mstform.getItem("phid_pc").getValue() && {
              clientSqlFilter: {
                bill_type: 1,
                phid_pc: mstform.getItem("phid_pc").getValue(),
              },
            }),
          });
        });
      }
    
    //获取合同数量
    function getCntCount(table, key, fun) {
        if (!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return;
        $NG.execServer("getCntCount", { type: 1, phid_pc: $NG.getCmpApi(table).getItem("phid_pc").getValue() }, (res) => {
          console.log(res, "count");
          switch (res.count) {
            case 0:
              return;
            case 1:
              const datas = JSON.parse(res.data)[0].extendObjects;
              const temp_map = {};
              temp_map[key] = datas.phid;
              temp_map[`${key}_EXName`] = datas.bill_name;
              $NG.updateState((updater) => {
                updater.data[table].setProps({
                  ...temp_map,
                });
                setTimeout(() => {
                  if (fun) {
                    fun();
                  }
                }, 200);
              });
          }
        });
      }

})  